package com.sugon.utils;

import cn.hutool.core.map.MapUtil;
import com.sugon.xss.SQLFilter;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 查询参数
 *
 * @author lipengjun
 * @date 2017年11月18日 下午13:13:23
 */
public class Query extends LinkedHashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    //当前页码
    private int page;
    //每页条数
    private int limit = 10;

    public Query(Map<String, Object> params) {
        this.putAll(params);
        //分页参数
        this.page = MapUtil.getInt(params, "page", 1);
        this.limit = MapUtil.getInt(params, "limit", 20);
        this.put("offset", (this.page - 1) * limit);
        this.put("page", this.page);
        this.put("limit", this.page * limit);
        if (params.containsKey("sidx") && params.get("sidx") != null && params.containsKey("order") && params.get("order") != null) {
            //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
            String sidx = params.get("sidx").toString();
            String order = params.get("order").toString();
            this.put("sidx", SQLFilter.sqlInject(sidx));
            this.put("order", SQLFilter.sqlInject(order));
        }

    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
